using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.TextEditor.Gui.CompletionWindow;

namespace ICSharpCode.TextEditor.Formatting.Intellisense
{
    public class XmlCompletionDataSupplier : ICompletionDataSupplier
    {
        private string HandleKeyWord(string lineText, char charTyped)
        {
            string empty = string.Empty;
            List<char> list = new List<char>();
            char[] array = (lineText + charTyped).ToCharArray();
            int num = array.Length - 1;
            while (num >= 0 && array[num] != ' ' && array[num] != '.' && array[num] != '>' && array[num] != '"')
            {
                list.Add(array[num]);
                num--;
            }
            list.Reverse();
            return string.Join("", list.ToArray()).Trim();
        }

        private string HandleKeyWord(string lineText)
        {
            string empty = string.Empty;
            List<char> list = new List<char>();
            char[] array = lineText.ToCharArray();
            int num = array.Length - 1;
            while (num >= 0 && array[num] != ' ' && array[num] != '.' && array[num] != '>' && array[num] != '"')
            {
                list.Add(array[num]);
                num--;
            }
            list.Reverse();
            return string.Join("", list.ToArray()).Trim();
        }

        public ICompletionData[] GetCompletionData(string lastkeyword, char charTyped)
        {
            ICompletionData[] source = new ICompletionData[11]
            {
                new XmlCompletionData("add", "", 0, SingleOneLine: true),
                new XmlCompletionData("appSettings", "", 0),
                new XmlCompletionData("configSections", "", 0),
                new XmlCompletionData("connectionString", "", 0),
                new XmlCompletionData("connectionStrings", "", 0),
                new XmlCompletionData("configuration", "", 0),
                new XmlCompletionData("startup", "", 0),
                new XmlCompletionData("section", "", 0, SingleOneLine: true),
                new XmlCompletionData("supportedRuntime", "", 0),
                new XmlCompletionData("handlers", "", 0),
                new XmlCompletionData("httpRuntime", "", 0)
            };
            ICompletionData[] source2 = new ICompletionData[14]
            {
                new DefaultCompletionData("connectionString", "", 1),
                new DefaultCompletionData("Data", "", 1),
                new DefaultCompletionData("id", "", 1),
                new DefaultCompletionData("key", "", 1),
                new DefaultCompletionData("value", "", 1),
                new DefaultCompletionData("version", "", 1),
                new DefaultCompletionData("MySql", "", 1),
                new DefaultCompletionData("name", "", 1),
                new DefaultCompletionData("providerName", "", 1),
                new DefaultCompletionData("System", "", 1),
                new DefaultCompletionData("SqlClient", "", 1),
                new DefaultCompletionData("targetFramework", "", 1),
                new DefaultCompletionData("true", "Boolean", 1),
                new DefaultCompletionData("false", "Boolean", 1)
            };
            ICompletionData[] source3 = new ICompletionData[6]
            {
                new NetVersionCompletionData("2.0", "", 0),
                new NetVersionCompletionData("3.5", "", 0),
                new NetVersionCompletionData("4.0", "", 0),
                new NetVersionCompletionData("4.5", "", 0),
                new NetVersionCompletionData("4.6", "", 0),
                new NetVersionCompletionData("5.0", "", 0)
            };
            lastkeyword = lastkeyword.Trim();
            if (!string.IsNullOrEmpty(lastkeyword))
            {
                string keyword = HandleKeyWord(lastkeyword, charTyped);
                IEnumerable<ICompletionData> enumerable = null;
                if (keyword.StartsWith("</"))
                {
                    keyword = keyword.Substring(2);
                    enumerable = source.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.OrdinalIgnoreCase));
                    foreach (XmlCompletionData item in enumerable)
                    {
                        item.CompletionTagClose(CompletionTagClosed: false);
                    }
                }
                else if (keyword.StartsWith("<"))
                {
                    keyword = keyword.Substring(1);
                    enumerable = source.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.OrdinalIgnoreCase));
                }
                else if (lastkeyword.IndexOf("targetFramework=") > 0)
                {
                    keyword = HandleKeyWord(lastkeyword) + charTyped;
                    enumerable = source3.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.CurrentCultureIgnoreCase));
                }
                else if (!string.IsNullOrEmpty(keyword))
                {
                    enumerable = source2.Where((ICompletionData w) => w.Text.StartsWith(keyword, StringComparison.OrdinalIgnoreCase));
                }
                if (enumerable != null && enumerable.Count() > 0)
                {
                    return enumerable.ToArray();
                }
            }
            return null;
        }
    }
}
